﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERP.Page;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class Bank_AccountUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertBank_Account();
            }
            else
            {
                UpdateBank_Account();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {

        }
        protected void InsertBank_Account()
        {
            try
            {
                var obj = new Bank_Account_ET
                {
                    Bank_Account_Name = txtTenTaiKhoan.Text,
                    Bank_Name = txtTenNganHang.Text,
                    Bank_Account_Date_Add = DateTime.Now,
                    Bank_Account_Date_Modify = DateTime.Now
                };
                Bank_Account_BLL.Inserttb_Bank_Account(obj);
                ((Bank_Account)Page).GetBank_Account();
                ClearTextBox();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateBank_Account()
        {
            try
            {
                var obj = new Bank_Account_ET
                {
                    Bank_Account_ID = int.Parse(lbID.Value),
                    Bank_Account_Name = txtTenTaiKhoan.Text,
                    Bank_Name = txtTenNganHang.Text,
                    Bank_Account_Date_Add = DateTime.Now,
                    Bank_Account_Date_Modify = DateTime.Now
                };
                Bank_Account_BLL.Updatetb_Bank_Account(obj);
                ((Bank_Account)Page).GetBank_Account();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtTenNganHang.Text = string.Empty;
            txtTenTaiKhoan.Text = string.Empty;
        }
        #endregion
    }
}